import os
import tarfile
import shutil

def extract_tar_gz(file_path, extract_to):
    with tarfile.open(file_path, 'r:gz') as tar:
        tar.extractall(path=extract_to)

def prompt_for_config(config_file):
    config = {}
    with open(config_file, 'r') as file:
        for line in file:
            if '=' in line:
                key, value = line.strip().split('=', 1)
                user_input = input(f"{key}? [{value}]: ")
                config[key] = user_input if user_input else value
    return config

def update_config_file(config_file, config):
    with open(config_file, 'w') as file:
        for key, value in config.items():
            file.write(f"{key}={value}\n")

def confirm_changes(config):
    print("\nConfiguration changes:")
    for key, value in config.items():
        print(f"{key}={value}")
    confirmation = input("Do you want to confirm these changes? (y/n): ")
    return confirmation.lower() == 'y'

def main():
    tar_gz_path = input("Enter the path to the .tar.gz file: ")
    extract_to = "./extracted_files"

    if not os.path.exists(extract_to):
        os.makedirs(extract_to)
        
    
    extract_tar_gz(tar_gz_path, extract_to)
    
    config_files = [f for f in os.listdir(extract_to) if f.endswith('.conf')]
    
    all_configs = {}
    for config_file in config_files:
        file_path = os.path.join(extract_to, config_file)
        configs = prompt_for_config(file_path)
        all_configs.update(configs)
    
    if confirm_changes(all_configs):
        for config_file in config_files:
            file_path = os.path.join(extract_to, config_file)
            update_config_file(file_path, all_configs)
        print("Installation completed successfully.")
    else:
        print("Installation aborted by user.")

if __name__ == "__main__":
    main()
